<!--  -->
<template>
  <el-card class="box-card" style="margin: 10px 0px;height:400px">
    <div slot="header" class="clearfix">
      <el-tabs v-model="activeName" class="tab-self">
        <el-tab-pane label="销售额" name="sale" />
        <el-tab-pane label="访问量" name="visite" />
      </el-tabs>
      <div class="self-right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          v-model="date"
          class="self-date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          value-format="yyyy-MM-dd"
        />
      </div>
    </div>
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <div ref="centerCharts" class="center-charts" />
        </el-col>
        <el-col :span="6" class="">
          <h3 style="margin: 0">门店{{ title }}排名</h3>
          <ul>
            <li v-for="store in rank" :key="store.no">
              <span :class="[store.no <= 3 ? 'rindex' : '']">{{ store.no }}</span>
              <span>{{ store.name }}</span>
              <span class="rvalue">{{ store.money }}</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import { mapState } from 'vuex'
import echarts from 'echarts'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      centerCharts: null,
      activeName: 'sale',
      date: [],
      rank: []
    }
  },
  computed: {
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: state => state.home.list
    })
  },
  watch: {
    title() {
      this.centerCharts.setOption({
        title: {
          text: this.title
        },
        xAxis: {
          data:
            this.title === '销售额'
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data:
              this.title === '销售额'
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
            color: 'skyblue'
          }
        ]
      })
      this.rank = this.title === '销售额' ? this.listState.orderRank : this.listState.userRank
    },
    listState() {
      this.rank = this.title === '销售额' ? this.listState.orderRank : this.listState.userRank
      this.centerCharts.setOption({
        title: {
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.listState.orderFullYear,
            color: 'skyblue'
          }
        ]
      })
    }
  },
  mounted() {
    this.centerChartsInit()
  },
  methods: {
    centerChartsInit() {
      this.centerCharts = echarts.init(this.$refs.centerCharts)
      this.centerCharts.setOption({
        title: {
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: [],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: [],
            color: 'yellowgreen'
          }
        ]
      })
    },
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style  scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab-self {
  width: 100%;
}
.self-right {
  position: absolute;
  right: 0;
}
.self-date {
  width: 250px;
  margin: 0 0px 0 20px;
}
.self-right span {
  margin: 0 10px;
}
.center-charts {
  width: 100%;
  height: 280px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0px;
}
ul li {
  height: 10%;
  margin: 10px 0px;
}
ul li span:nth-child(1) {
  display: inline-block;
  width: 20px;
  height: 20px;
  text-align: center;
}
.rindex {
  float: left;
  border-radius: 50%;
  background: black;
  color: white;
}
.rvalue {
  float: right;
}
</style>
